//
//  RBDaylySignVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/15.
//

#import "RBDaylySignVC.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "UIViewController+RBAdd.h"
#import "RBNavigationBar.h"
#import <FSCalendar.h>
#import <NSDate+YYAdd.h>
#import "RBDaylySignDateCell.h"
#import "RBDaylySignSucPopVC.h"
#import "RBDaylySignRuleVC.h"
#import "RBDaylySignResponse.h"

@interface RBDaylySignVC ()<FSCalendarDataSource, FSCalendarDelegate>

@property(nonatomic, strong)RBNavigationBar *navigationBar;
@property(nonatomic, strong)UIScrollView *scrollView;

@property(nonatomic, strong)UILabel *dateL;
@property(nonatomic, strong)FSCalendar *calendar;

@property(nonatomic, strong) RBDaylySignResponse *response;

@end

@implementation RBDaylySignVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    self.navigationBar.hidden = NO;
    self.statusBarStyle = UIStatusBarStyleLightContent;
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = [RBThemeConfig buttonBlueColor];
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:0 text:@"已坚持"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            
            NSInteger day = self.response.data.qiandao_lianxu_tian;
            NSInteger hundred = day/100;
            day = day%100;
            NSInteger ten = day/10;
            day = day%10;
            
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(40) weight:2 color:[RBThemeConfig buttonBlueColor] align:1 text:[NSString stringWithFormat:@"%zd", hundred] bgColor:UIColor.whiteColor radius:Zoom(6)] withHeight:CGFLOAT_MIN width:Zoom(45) top:0 bottom:0 left:Zoom(8) right:0 centerY:0];
            
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(40) weight:2 color:[RBThemeConfig buttonBlueColor] align:1 text:[NSString stringWithFormat:@"%zd", ten] bgColor:UIColor.whiteColor radius:Zoom(6)] withHeight:CGFLOAT_MIN width:Zoom(45) top:0 bottom:0 left:Zoom(4) right:0 centerY:0];
            
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(40) weight:2 color:[RBThemeConfig buttonBlueColor] align:1 text:[NSString stringWithFormat:@"%zd", day] bgColor:UIColor.whiteColor radius:Zoom(6)] withHeight:CGFLOAT_MIN width:Zoom(45) top:0 bottom:0 left:Zoom(4) right:0 centerY:0];
            
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:0 text:@"天签到"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(8) right:0 centerY:0];
            view;
        }) withHeight:Zoom(60) width:CGFLOAT_MIN top:Zoom(17) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        [view view_addView:[RanTool labelWithFontSize:Zoom(12) weight:0 color:UIColor.whiteColor align:1 text:@"登录后将保留累计签到天数"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(15) bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            view.backgroundColor = [UIColor colorWithHexString:@"#1656D3"];
            [view rbSetCornerRadius:Zoom(20)];
            
            [view h_view_addView:[RanTool imgViewWithMode:0 radius:0 imgName:@"signGold"] withHeight:Zoom(23) width:Zoom(23) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(14) right:0 centerY:YES];
            
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(14) weight:2 color:UIColor.whiteColor align:1 text:self.response.data.qiandao_jiangli] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(0) right:Zoom(37) centerY:0];
            
            view;
        }) withHeight:Zoom(40) width:Zoom(306) top:Zoom(24) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        [view view_addView:({
            UIView *view = UIView.new;
            for (NSInteger i = 0; i < 15; i++)
            {
                [view h_view_addView:({
                    UIView *view = UIView.new;
                    view.backgroundColor = UIColor.whiteColor;
                    [view rbSetCornerRadius:Zoom(3)];
                    view;
                }) withHeight:CGFLOAT_MIN width:Zoom(6) top:0 bottom:0 left:Zoom(16) right:CGFLOAT_MIN centerY:0];
            }
            view;
        }) withHeight:Zoom(51) width:CGFLOAT_MIN top:Zoom(33) bottom:0 left:Zoom(12) right:0 centerX:NO];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        
        [view view_addView:({
            UIView *view = UIView.new;
            
            [view h_view_addView:({
                UIImageView *view = [RanTool imgViewWithMode:1 radius:0 imgName:@"signPre"];
                [view extendResponseArea];
                [view rbAddTapGesWithTarget:self action:@selector(previousBtnTapped)];
                view;
            }) withHeight:Zoom(11) width:Zoom(11) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(25) right:0 centerY:YES];
            
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(20) weight:2 color:[RBThemeConfig textBlackColor] align:1 text:[NSDate.date stringWithFormat:@"yyyy年MM月"]];
                self.dateL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            
            [view h_view_addView:({
                UIImageView *view = [RanTool imgViewWithMode:1 radius:0 imgName:@"signNext"];
                [view extendResponseArea];
                [view rbAddTapGesWithTarget:self action:@selector(nextBtnTapped)];
                view;
            }) withHeight:Zoom(11) width:Zoom(11) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:0 right:Zoom(25) centerY:YES];
            
            view;
        }) withHeight:Zoom(60) width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:self.calendar withHeight:Zoom(310) width:CGFLOAT_MIN top:Zoom(0) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
        
        if([self.response.data.qiandao_today isEqualToString:@"N"])
        {
            [view view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:1 text:@"立即签到" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(25)];
                [view rbAddTapGesWithTarget:self action:@selector(signBtnTapped)];
                view;
            }) withHeight:Zoom(50) width:Zoom(304) top:Zoom(24) bottom:CGFLOAT_MIN left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        }
        else
        {
            [view view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:1 text:@"今日已签到" bgColor:[RBThemeConfig textLightGrayColor] radius:Zoom(25)];
                view;
            }) withHeight:Zoom(50) width:Zoom(304) top:Zoom(24) bottom:CGFLOAT_MIN left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        }
        
        view;
    }) withHeight:Zoom(489) width:Zoom(350) top:-Zoom(44) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    [self.calendar reloadData];
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"hu_qiandao" post:YES log:YES responseClass:RBDaylySignResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(RBDaylySignResponse *data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(RBDaylySignResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event

- (void)previousBtnTapped
{
    self.calendar.currentPage = [self.calendar.currentPage dateByAddingMonths:-1];
}

- (void)nextBtnTapped
{
    self.calendar.currentPage = [self.calendar.currentPage dateByAddingMonths:1];
}

- (void)signBtnTapped
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"hu_qiandao_sub" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
        RBDaylySignSucPopVC *vc = RBDaylySignSucPopVC.new;
        vc.type = weak_self.response.data.type;
        vc.day = weak_self.response.data.qiandao_lianxu_tian + 1;
        vc.points = weak_self.response.data.qiandao_today_jiangli;
        [weak_self presentVc:vc];
        [weak_self getData];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

- (void)questionBtnTapped
{
    RBDaylySignRuleVC *vc = RBDaylySignRuleVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - FSCalendarDelegate

- (FSCalendarCell *)calendar:(FSCalendar *)calendar cellForDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)position
{
    RBDaylySignDateCell *cell = [calendar dequeueReusableCellWithIdentifier:@"cell" forDate:date atMonthPosition:position];
    cell.day = [date isToday]? @"今": [NSString stringWithFormat:@"%zd", date.day];
    cell.inMonth = (date.year == calendar.currentPage.year) && (date.month == calendar.currentPage.month);
    if (cell.inMonth)
    {
        cell.sign = [self.response.data.qiandao_timeArray containsObject:[date stringWithFormat:@"yyyy-MM-dd"]];
    }
    else
    {
        cell.sign = NO;
    }
    return cell;
}

- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    return NO;
}

- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar
{
    self.dateL.text = [calendar.currentPage stringWithFormat:@"yyyy年MM月"];
    [self.calendar reloadData];
}


#pragma mark - Setter

#pragma mark - Getter

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *view = RBNavigationBar.new;
            view.needGradientView = NO;
            view.backgroundAlpha = 1;
            view.bgColor = [RBThemeConfig buttonBlueColor];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            
            [view.naviBar addBackBtn];
            
            UILabel *titleL = [RanTool labelWithFontSize:18 weight:0 color:UIColor.whiteColor align:0 text:@"每日签到"];
            [view.naviBar addSubview:titleL];
            [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(0);
            }];
            
            UILabel *rightL = UILabel.new;
            rightL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
                UIFont *font = [UIFont systemFontOfSize:13];
                [maker addAttributeStringWithFont:font color:UIColor.whiteColor string:@"活动规则 "];
                [maker addAttributeStringWithImage:[UIImage imageNamed:@"signQuestion"] imageSize:CGSizeMake(17, 17) maxFont:font];
            }];
            [rightL rbAddTapGesWithTarget:self action:@selector(questionBtnTapped)];
            [view.naviBar addSubview:rightL];
            [rightL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.right.mas_equalTo(-Zoom(12));
            }];
            
            view;
        });
    }
    return _navigationBar;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.showsVerticalScrollIndicator = NO;
            view.bounces = NO;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.navigationBar.mas_bottom);
            }];
            view;
        });
    }
    return _scrollView;
}

- (FSCalendar *)calendar
{
    if (!_calendar)
    {
        self.calendar = ({
            FSCalendar *view = FSCalendar.new;
            view.dataSource = self;
            view.delegate = self;
            view.headerHeight = 0;
            view.appearance.weekdayTextColor = [RBThemeConfig textBlackColor];
            view.appearance.caseOptions =  FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
            view.appearance.todayColor = nil;
            view.appearance.titleTodayColor = nil;
            [view registerClass:RBDaylySignDateCell.class forCellReuseIdentifier:@"cell"];
            view;
        });
    }
    return _calendar;
}

@end
